package cn.itsource.basic.service.Impl;

import cn.itsource.basic.basicMapper.BaseMapper;
import cn.itsource.basic.domain.BaseDomain;

import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.IBaseService;
import cn.itsource.basic.util.PageList;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public abstract class   BaseSerivcelmpl<T extends BaseDomain,Q extends BaseQuery> implements IBaseService<T,Q> {
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public void save(T t) {

        baseMapper.updateByPrimaryKeySelective(t);
    }

    @Override
    public void update(T t) {
        baseMapper.updateByPrimaryKeySelective(t);
        this.save(t);
    }


    @Override
    public void delete(Long id) {
        baseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<T> findAll() {

        return baseMapper.selectAll();
    }

    @Override
    public T findOne(Long id) {
        return baseMapper.selectOneByExample(id);
    }


    @Override
    public PageList<T> findPage(Q q) {
        //分页插件!!!!
        PageHelper.startPage(q.getPage(), q.getSize());
        List<T> page = baseMapper.selectAll();
        //封装到pageInfo
        PageInfo<T> info = new PageInfo<>(page);
        //封装自定义对象
        PageList<T> pageList = new PageList<>(info.getTotal(), info.getList());
        return pageList;
    }

    @Override
    public void move(Q q) {
        if (q.getIds() == null || q.getIds().length == 0) {
            return;
        }
        baseMapper.move(q.getIds());
    }
}

